<!DOCTYPE html>
<html>
<head>
	<title>角色权限维护</title>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script type="text/javascript">
	    var path = window.document.location.href;
		var pathName = window.document.location.pathname;
		var hostPath = path.substring(0, path.indexOf(pathName));
		var projectName = pathName.substring(0, pathName.substr(1).indexOf("/") + 1);
		document.write('<scr' + 'ipt src="' + projectName + '/js/html/header.js"></scr'+'ipt>');
    </script>
</head>
<body>
	<div style="width:100%">
		<!-- 左边是角色列表 -->
		<div style="float:left;width:49%">
			<table id="dg" style="height:auto;width:90%">
				<thead>
					<tr>
						<th field="id">角色ID</th>
						<th field="roleName"  width="40%" align="right"
							data-options="
							sortable:true,
							remoteSort:false,
							editor:{
								type:'validatebox',
								options:{
									required:true
								}
							}">角色名称</th>
						
						<th field="status"  width="40%" align="right"
							data-options="
							sortable:true,
							remoteSort:false,
							editor:{
								type:'validatebox',
								options:{
									required:true
								}
							}"
							>角色状态</th>
					</tr>
				</thead>
			</table>
		<div id="dg-buttons" style="height:auto">
			<div style="margin-bottom:5px">
				<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save',plain:true" 	onclick="saveReso()">保存</a>
			</div>
		</div>
		</div>
			<!-- 右边是权限列表 -->
			<div style="float:left">
			<ul id="home-tree"></ul> 
		</div>  
	</div>
	
	<script type="text/javascript">
	
	$(function(){
	    $('#home-tree').tree({
	    	url : projectName+'/authset/role/resource?roleId='+0,
	        checkbox:true
	    });
	});
	
	//重新加载权限树
	function treeReLoad(rowIndex, rowData){
		roleId = rowData['id'];
		$('#home-tree').tree("options").url=projectName+'/authset/role/resource?roleId='+roleId;
		$('#home-tree').tree("reload");
	}
	
	
	//保存修改后的权限树
	function saveReso(){
		var roleId;
		var rowData=$('#dg').datagrid('getSelected');
		if(rowData!=null){
			roleId=rowData['id'];
		}else{
			var rows=$('#dg').datagrid('getRows');
			rowData=rows[0];
			roleId=rowData['id'];
		}
		
		//获取新增节点，新增节点是被选中但是没有keyID的
		var nodes=$('#home-tree').tree('getChecked');
		var nodeList=new Array();
		for (var i=0;i<nodes.length;i++){
			var node=new Object();
			var tmp=nodes[i];
			if((tmp.attributes.keyID==0 || typeof(tmp.attributes.keyID)=="undefined")){
				node.id=tmp.attributes.keyID;
				node.resId=tmp.id;
				node.roleId=roleId;
				nodeList.push(node);
			}
		}
		
		//未选中但有主键的为需要删除的
		var dnodes=$('#home-tree').tree('getChecked','unchecked');
		var deleteNodes=new Array();
		var deleteNode=new Object();
		for (var i=0;i<dnodes.length;i++){
			var tmp=dnodes[i];
			if(typeof(tmp.attributes.keyID)!="undefined" && tmp.attributes.keyID!=0){
				var node=new Object();
				node.id=tmp.attributes.keyID;
				node.resId=tmp.id;
				node.roleId=roleId;
				deleteNodes.push(node);
			}
		}
		if(jQuery.isEmptyObject(nodeList) && jQuery.isEmptyObject(deleteNodes)){
			$.messager.alert("系统提示", "权限无变更，无需保存！");
			return false;
		}
		$.ajax({
			type:"POST",
			url:projectName+'/authset/role/resource/update',
			data:{
				"insertNodes":JSON.stringify(nodeList),
				"deleteNodes":JSON.stringify(deleteNodes)
			},
			dataType : "json",
			success:function(data){
				$.messager.alert("系统提示", "保存成功！");
			}
		});
	}

	
	var initd = {
		queryDataURL:"/system/roleinfo/query",//查询URL
		saveDataURL:"/system/roleinfo/save",//保存URL
		removeDataURL:"/system/roleinfo/remove",//删除URL
		dg:$('#dg'),
		onClickRow: treeReLoad,
		dataDicList:{'STATUS':['status']}//需加载的 DATADIC 字典名+作用的字段名
	};

</script>
</body>


</html>

